package com.ctvit.wsservice.basedata.service;


import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.service.BaseService;
import com.ctvit.wsservice.basedata.domain.EipMessageBatchPub;
import com.ctvit.wsservice.basedata.domain.EipMessageMember;
import com.ctvit.wsservice.basedata.domain.EipMessageQueryPub;
import com.ctvit.wsservice.basedata.domain.EipMessageQuerySub;
import org.apache.log4j.Logger;
import org.springframework.jdbc.BadSqlGrammarException;

import java.util.List;

/**
 * Created by LoreYun on 2016/4/26.
 */
public class EIPMessageProcessService extends BaseService {
	private static Logger logger = Logger.getLogger(EIPMessageProcessService.class);

	/**
	 * 添加短信到队列
	 *
	 * @param pk
	 * @return
	 */
	public boolean doGeneSMS(Long pk) {
		logger.debug("doGeneSMS:pk:" + pk);

		try {
		} catch (BadSqlGrammarException exception) {
			exception.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 添加某一个系统所发送的短信总数
	 *
	 * @param member
	 * @param totalQuantity
	 * @return
	 */
	public int updateUsedAmout(EipMessageMember member, Integer totalQuantity) {
		StringBuffer sql = new StringBuffer("UPDATE t_eip_message_member SET f_used_amout=f_used_amout+");
		sql.append(totalQuantity).append(" ").append("WHERE f_member_code='").append(member.getMemberCode()).append("';");

		return genericDao.updateBySqlStr(sql.toString());
	}

	/**
	 * 根据memberCoed查找member信息
	 *
	 * @param memberCode
	 * @return
	 */
	public EipMessageMember findByMemberCode(String memberCode) {
		logger.debug("findByMemberCode:memberCode:" + memberCode);

		Condition condition = new Condition();
		condition.add("memberCode", memberCode);
		List<EipMessageMember> result = genericDao.getByCondition(EipMessageMember.class, condition, null);
		if (result.size() > 0) {
			return result.get(0);
		} else {
			logger.debug("findByMemberCode:未找到对应条目");
			return null;
		}
	}

	/**
	 * 向t_eip_message_query_sub 表 中差插入数据
	 *
	 * @param querySub
	 * @return
	 */
	public Long insertEIPMessageQuerySub(EipMessageQuerySub querySub) {
		return genericDao.insert(querySub);
	}

	/**
	 * 向t_eip_message_query_pub 表 中差插入数据
	 *
	 * @param queryPub
	 * @return
	 */
	public Long insertEIPMessageQueryPub(EipMessageQueryPub queryPub) {
		return genericDao.insert(queryPub);
	}

	/**
	 * 更新
	 * @param queryPub
	 * @return
	 */
	public Integer updateEIPMessageQueryPub(EipMessageQueryPub queryPub) {
		if (queryPub == null) {
			return -1;
		}
		return genericDao.updateExclusive(queryPub, new String[]{"createTimestamp", "modifyTimestamp"});
	}

	/**
	 * 向t_eip_message_batch_pub 表 中差插入数据
	 *
	 * @param batchPub
	 * @return
	 */
	public Long insertEIPMessageBatchPub(EipMessageBatchPub batchPub) {
		return genericDao.insert(batchPub);
	}
}
